<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16" type="flex" justify="center">
      <a-col :order="isMobile ? 2 : 1" :md="24" :lg="16">

        <a-form layout="vertical" :form="form">
			<!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
			<a-form-item v-show="false" label="主键ID">
			  <a-input v-decorator="['id', { initialValue: null }]" disabled />
			</a-form-item>
			
          <a-form-item
            label="企业名称"
			required
          >
          <a-input placeholder="请输入企业名称" v-decorator="['name',{rules: [{required: true, message: '请输入企业名称!'}]}]"/>
          </a-form-item>
		  
          <a-form-item
            label="企业类型"
			required
          >
            <a-select  placeholder="请选择" default-value="私企" v-decorator="['type',{rules: [{required: true, message: '请选择!'}]}]">
              <a-select-option value="私企">私企</a-select-option>
              <a-select-option value="国企">国企</a-select-option>
			  <a-select-option value="上市公司">上市公司</a-select-option>
			  <a-select-option value="跨国公司">跨国公司</a-select-option>
            </a-select>
          </a-form-item>

		  <a-form-item
		    label="员工数量"
		  	required
		  >
		  <a-input placeholder="请输入员工数量" type="number" v-decorator="['workers',{rules: [{required: true, message: '请输入员工数量!'}]}]"/>
		  </a-form-item>
		  
          <a-form-item
            label="所属行业"
            required
          >
            <a-input placeholder="请输入所属行业" v-decorator="['industry',{rules: [{required: true, message: '请输入所属行业!'}]}]"/>
          </a-form-item>

		  
	
		<a-form-item
		  label="官网"
		>
		  <a-input placeholder="请输入官网" v-decorator="['website']"/>
		</a-form-item>
		  
		  
		  <a-form-item
		    label="公司介绍"
			required
		  >
		    <a-textarea v-decorator="['desp',{rules: [{required: true, message: '请输入介绍!'}]}]"></a-textarea>
		  </a-form-item>
		  
          <a-form-item>
            <a-button type="primary" @click="updateInfo()">更新公司信息</a-button>
          </a-form-item>
        </a-form>

      </a-col>
    </a-row>

  </div>
</template>

<script>
import { baseMixin } from '@/store/app-mixin'
import {updateCompany,findAccount} from '@/api/account'
import { LOGIN_ID } from '@/store/mutation-types'
import storage from 'store'
import pick from 'lodash.pick'
// 表单字段
const fields = ['desp', 'id', 'name', 'type', 'website','industry','workers']
export default {
  name: 'CompanyInfo',
  mixins: [baseMixin],
  components: {

  },
  data () {
    return {
      // cropper
	  model: {},
      preview: {},
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      },
	  form: this.$form.createForm(this)
    }
  },
  created() {
	  findAccount(storage.get(LOGIN_ID)).then(res=> {
		  if(res.code == 200){
			  this.model = res.data.company
		  }
	  })
	  
	  // 当 model 发生改变时，为表单设置值
	  this.$watch('model', () => {
	    this.model && this.form.setFieldsValue(pick(this.model, fields))
	  })
  },
  methods: {
	updateInfo(){
		this.form.validateFields((errors, values) => {
		  if (!errors) {
		    console.log('values', values)
			const data = {
				account: storage.get(LOGIN_ID),
				company: values
			}
			updateCompany(data).then(res=> {
				if(res.code == 200){
					this.$message.info('修改成功')
				}else{
					this.$message.error('修改失败')
				}
			}).catch(err => {
				this.$message.error('修改失败')
			})
		  } 
		})
	}
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
